<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>课堂签到</title>
    <link rel="shortcut icon" href="../../static/img/icon.svg">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="../../static/html5up-forty/assets/css/main.css" />
    <noscript><link rel="stylesheet" href="../../static/html5up-forty/assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<div id="wrapper">

    <!-- Header -->
    <header id="header" class="alt">
        <a th:href="@{|/templates/manage|}" class="logo"><strong>Jli</strong> <span>by my</span></a>
        <nav>
            <p style="font-size: 15px" th:text="${session.teacher.getName()} + 老师"></p>
            <a href="#menu">菜单</a>
        </nav>
    </header>

    <!-- Menu -->
    <nav id="menu">
        <ul class="links">
            <li><a th:href="@{|/index|}">主页</a></li>
            <li><a href="#">编辑信息</a></li>
            <li><a href="#">修改密码</a></li>
        </ul>
        <ul class="actions stacked">
            <li><a th:href="@{|/teacherLogout|}" class="button fit">退出登录</a></li>
        </ul>
    </nav>

    <!-- Main -->
    <div class="main">
        <div class="inner" style="width: 25%;">
            <section>
                <div class="fields">
                    <div class="field">
                        <a href="javascript:void(0)" class="button" onclick="getSignCode()">发起签到</a>
                        <a th:href="@{|/manage|}" class="button primary">返回首页</a>
                    </div>

                    <div class="field">
                        <h1 id="code">签到码：</h1>
                        <h2 id="signStuCount">已签到人数：</h2>
                        <h2 id="remainingTime">剩余时间：</h2>
                    </div>
                </div>

            </section>
        </div>
    </div>
</div>

<!-- Scripts -->
<script src="../../static/html5up-forty/assets/js/jquery.min.js"></script>
<script src="../../static/html5up-forty/assets/js/jquery.scrolly.min.js"></script>
<script src="../../static/html5up-forty/assets/js/jquery.scrollex.min.js"></script>
<script src="../../static/html5up-forty/assets/js/browser.min.js"></script>
<script src="../../static/html5up-forty/assets/js/breakpoints.min.js"></script>
<script src="../../static/html5up-forty/assets/js/util.js"></script>
<script src="../../static/html5up-forty/assets/js/main.js"></script>
<script>
    //生效开始时间
    let startDateTime;
    //剩余时间
    let remainingTime;

    //定时执行获取剩余生效时间
    let interval = setInterval(getRemainingTime, 1000);
    //循环获取已签到人数
    let interval1 = setInterval(function (){
         $.ajax({
             type: "GET",
             url: "/webapi/teacher/getSignCount",
         }).done(function (data){
             if (data === -1){
                 $("#signStuCount").text("已签到人数：0");
             } else {
                 $("#signStuCount").text("已签到人数：" + data);
             }
         })
    }, 1000);

    function getRemainingTime() {
        //当前时间
        let currentDateTime = Date.now();
        //剩余时间
        remainingTime = 120000 - (currentDateTime - startDateTime);
        console.log("签到码剩余生效时间：" + remainingTime / 1000 + "秒");
        if (remainingTime > 0){
            $("#remainingTime").text("剩余时间：" + Math.floor(remainingTime / 1000) + "s");
            return remainingTime;
        } else {//剩余时间小于0时结束定时器
            clearInterval(interval);
            clearInterval(interval1);
            return -1;
        }
    }
    function getSignCode() {

        if (startDateTime !== undefined && getRemainingTime() > 0){
            alert("上一个验证码还没有失效")
        } else {
            //当前时间
            let currentDateTime = Date.now();
            $.ajax({
                type: "GET",
                url: "/webapi/teacher/getSignCode",
                data: {
                    currentDateTime:currentDateTime
                }
            }).done(function (signCodeUtil) {
                let code = signCodeUtil.code;
                startDateTime = signCodeUtil.startDate;
                //console.log(code);
                $("#code").text("签到码：" + code);
            });
        }
    }
    function setSignCodeUtil() {

    }

</script>
</body>
</html>